const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}
const formatDate = timestamp => {
    const date = new Date(timestamp);;
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    let dateString = year + "年" + month + "月" + day + "日"
    return dateString;
}

const formatDate_old = timestamp => {
    const date = new Date(timestamp);;
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    let dateString = year + "-" + month + "-" + day
    return dateString;
}

const navigaback = () => {
    wx.navigateBack()
}
const swiperindex = () => {
    wx.switchTab({
        url: '../pages/index/index',
    })
}
const copydata = (string) => {
    wx.setClipboardData({
        data: string,
        success(res) {
            
        }
    })
}


/*函数防抖*/
let  debounce = (fn, interval)=> {
    var timer;
    var gapTime = interval || 1200;//间隔时间，如果interval不传，则默认1000ms
    return function () {
        clearTimeout(timer);
        var context = this;
        var args = arguments;//保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
        timer = setTimeout(function () {
            fn.call(context, args[0]);
            // return context.fn
            // 函数不执行
        }, gapTime);
    };
};

/*函数节流*/
let throttle=(fn, interval, prompt)=> {
    var enterTime = 0;//触发的时间
    var gapTime = interval || 6000;//间隔时间，如果interval不传，则默认300ms

    return function () {
        var context = this;
        var backTime = new Date();//第一次函数return即触发的时间
        if (backTime - enterTime > gapTime) {
            fn.call(context, arguments[0]);
            enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
        } else {
            if (backTime - enterTime > prompt) {
                // wx.showToast({
                //     title: '点击过快',
                //     icon: 'none',
                //     duration: 2000
                // })
            }
        }
    };
};
module.exports = {
    formatTime,
    formatDate,
    navigaback,
    swiperindex,
    copydata,
    debounce,
    throttle,
    formatDate_old,
}
